package com.example.myapplication2;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final  String TAG="MainActivity";
    String jsonstr="[\n"+
            "  {\"id\":\"1\",\"name\":\"张三\",\"age\":\"18\" },\n"  +
            "  {\"id\":\"2\",\"name\":\"李四\",\"age\":\"18\" },\n"  +
            "  {\"id\":\"3\",\"name\":\"王五\",\"age\":\"18\"}\n"  +
            "]";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray=new JSONArray(jsonstr);
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        String id=jsonObject.getString("id");
                        String name=jsonObject.getString("name");
                        String age=jsonObject.getString("age");
                        Log.e(TAG,"id="+id+"name ="+name+"age ="+age);
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        });
        final Student s1=new Student("张三",20,"1712002");
        final Student s2=new Student("张4",20,"1712002");
        final Student s3=new Student("张5",20,"1712002");
        final ArrayList<Student> studentArrayList=new ArrayList<>();
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("name",s1.getName());
                    jsonObject.put("age",s2.getAge());
                    jsonObject.put("stuNo",s3.getStuNo());
                    Log.e(TAG,"jsonObject = "+jsonObject.toString());
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    JSONArray jsonArray=new JSONArray();
                    for (Student student:studentArrayList){
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("name",student.getName());
                        jsonObject.put("age",student.getAge());
                        jsonObject.put("stuNo",student.getStuNo());
                        jsonArray.put(jsonObject);
                    }
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("stuList",jsonArray);
                    Log.e(TAG,"jsonObject = "+jsonObject.toString());
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    InputStream inputStream=getResources().getAssets().open("data.txt");

                    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                    StringBuffer stringBuffer=new StringBuffer();
                    String line=bufferedReader.readLine();
                    while (line !=null){
                        stringBuffer.append(line);
                        line=bufferedReader.readLine();
                    }
                    Log.e(TAG,"data =" + stringBuffer.toString());
                    JSONArray jsonArray=new JSONArray(stringBuffer.toString());
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        String province=jsonObject.getString("name");
//                       Log.e(TAG,"provice="+province);

                        if (province.equals("广东省")){
                            JSONArray cityArray=jsonObject.getJSONArray("city");
                            for (int j=0;j<cityArray.length();j++){
                                JSONObject cityObject=cityArray.getJSONObject(j);
                                String cityName=cityObject.getString("name");
                                Log.e(TAG,"cityName="+cityName);
                                if (cityName.equals("广州市")){
                                    JSONArray areaArry=cityObject.getJSONArray("area");
                                    for (int k=0;k<areaArry.length();k++){
                                        String areaName=areaArry.getString(k);
                                        Log.e(TAG,"area="+areaName);
                                    }
                                }
                            }
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
